package com.shumei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shumei.dao.mapper.HrLoginMapper;
import com.shumei.dao.pojo.Eq;
import com.shumei.dao.pojo.HrInformation;
import com.shumei.dao.pojo.HrLogin;
import com.shumei.service.HrInformationService;
import com.shumei.service.HrLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class HrLoginServiceImpl implements HrLoginService {
    @Autowired
    HrLoginMapper hrLoginMapper;
    @Autowired
    EqServiceImpl eqService;
    @Autowired
    HrInformationService hrInformationService;
    @Override
    public void HrRegist(String accountnum, String password, HrInformation hrInformation, Eq eq) {
      Long id =getid(accountnum);
        System.out.println(id);
        if (id!=null){
            System.out.println("该用户已存在");
        }else {
            HrLogin hrLogin=new HrLogin();
            hrLogin.setAccountnum(accountnum);
            hrLogin.setPassword(password);
            hrLogin.setCreateTime(new Date());
            int result=hrLoginMapper.insert(hrLogin);

            if (result>0){

                eqService.insertEq(eq);
                Long id2 =getid(accountnum);
                hrInformation.setHrId(id2);
                Eq eqdata=eqService.selectByname(eq.getEqName());
                int eqid=eqdata.getId();
                hrInformation.setEqId(eqid);
                hrInformationService.insertHrInformation(hrInformation);
                System.out.println("注册成功");
            }
            else {
                System.out.println("注册失败");
            }
        }



    }

    @Override
    public Long getid(String accountnum) {
    Long id=hrLoginMapper.getHrid(accountnum);
       return id;
    }

    @Override
    public void eqExistHrRegist(String accountnum, String password, HrInformation hrInformation, String eqname) {
        Long id =getid(accountnum);
        System.out.println(id);
        if (id!=null){
            System.out.println("该用户已存在");
        }else {
            HrLogin hrLogin=new HrLogin();
            hrLogin.setAccountnum(accountnum);
            hrLogin.setPassword(password);
            hrLogin.setCreateTime(new Date());
            int result=hrLoginMapper.insert(hrLogin);

            if (result>0){


                Long id2 =getid(accountnum);
                hrInformation.setHrId(id2);
                Eq eqdata=eqService.selectByname(eqname);
                int eqid=eqdata.getId();
                hrInformation.setEqId(eqid);
                hrInformationService.insertHrInformation(hrInformation);
                System.out.println("注册成功");
            }
            else {
                System.out.println("注册失败");
            }
        }
    }

    @Override
    public HrInformation login(String accountnum, String password) {
        QueryWrapper<HrLogin> wrapper=new QueryWrapper<HrLogin>();
        wrapper.eq("accountnum",accountnum).eq("password",password);
        HrLogin hrLogin=hrLoginMapper.selectOne(wrapper);
        HrInformation hrInformation=hrInformationService.selectByHrId(hrLogin.getHrId());
        return hrInformation;

    }
}
